<?php      
session_start();         
include('system/connect.php');

$oponent_id = (int)$_GET['uid'];
$room_id = (int)$_GET['rid']; 
$my_id = $_SESSION['my_id'];

$sql = "SELECT
        chat2_room.is_active
        FROM
        chat2_room_users
        Inner Join chat2_room ON chat2_room_users.room_id = chat2_room.id
        WHERE
        chat2_room_users.user_id =  '$my_id' AND chat2_room_users.room_id =  '$room_id' 
        ";
$room = mysql_query($sql); 
$result = array();
while ($row = mysql_fetch_assoc($room)) {
   $result[] = $row;
}   
//print_r($result);                       
if (!empty($result)) {  
    if ($result[0]['is_active'] != '1') {         
        mysql_query("delete from `chat2_room` where `chat2_room`.`id` = '$room_id'");
        mysql_query("delete from `chat2_room_users` where `chat2_room_users`.`room_id` = '$room_id'");
        //TODO - Delete user chat  assosiations`
    } else {             
       mysql_query("update `chat2_room` set `chat2_room`.`is_active` = '$my_id' where `chat2_room`.`id` = '$room_id'");
    }      
}
?>
